﻿using Engine;
using System.Collections.Generic;

namespace CommandBlock
{
    /// <summary>
    /// 录制管理
    /// </summary>
    public class RecordManager
    {
        public class RecordPlayerAction
        {
            public string ActionName;

            public float ActionTime;

            public Point3 Point;

            public int Value;

            public void MakeAction(ComponentPlayer componentPlayer)
            {
                componentPlayer.m_subsystemTerrain.DestroyCell(0, Point.X, Point.Y, Point.Z, Value, noDrop: true, noParticleSystem: false);
                componentPlayer.m_subsystemAudio.PlaySound("Audio/BlockPlaced", 1f, 0f, new Vector3(Point.X, Point.Y, Point.Z), 5f, autoDelay: false);
                componentPlayer.Entity.FindComponent<ComponentHumanModel>().m_handAngles2 = new Vector2(-0.2f, 0.3f);
            }
        }

        public static Dictionary<Point3, int> ChangeBlocks = new Dictionary<Point3, int>();

        public static List<RecordPlayerStats> RecordPlayerStats = new List<RecordPlayerStats>();

        public static List<RecordPlayerAction> RecordPlayerActions = new List<RecordPlayerAction>();

        public static bool Recording = false;

        public static bool Replaying = false;

        public static float FrameTime = 0.01f;

        public static int StatsIndex = 0;

        public static int ActionIndex = 0;

        public static Vector3 FirstPosition;

        public static Vector3 FirstDirection;

        public static float FirstTime;

        public static float ReplayTime;

        public static void Replay(ComponentPlayer componentPlayer, float dt)
        {
            ReplayTime += dt;
            if (StatsIndex >= RecordPlayerStats.Count)
            {
                Replaying = false;
                componentPlayer.GameWidget.ActiveCamera = componentPlayer.GameWidget.FindCamera<FppCamera>(true);
            }
            RecordPlayerStats record = RecordPlayerStats[StatsIndex];
            if (record.Time - FirstTime - ReplayTime <= 0.02f)
            {
                componentPlayer.ComponentBody.Position = record.Position;
                componentPlayer.ComponentBody.Rotation = record.Rotation;
                componentPlayer.ComponentLocomotion.LookAngles = record.LookAngles;
                int slot = componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex;
                int count = componentPlayer.ComponentMiner.Inventory.GetSlotCount(slot);
                componentPlayer.ComponentMiner.Inventory.RemoveSlotItems(slot, count);
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(slot, record.ActiveBlockValue, count);
                componentPlayer.ComponentLocomotion.m_flying = record.Flying;
                componentPlayer.ComponentLocomotion.m_walking = record.Walking;
                componentPlayer.ComponentBody.IsSneaking = record.Sneaking;
                StatsIndex++;
            }
            while (ActionIndex < RecordPlayerActions.Count)
            {
                float time = RecordPlayerActions[ActionIndex].ActionTime - FirstTime - ReplayTime;
                if (time <= 0.02f)
                {
                    RecordPlayerActions[ActionIndex].MakeAction(componentPlayer);
                    ActionIndex++;
                }
                else break;
            }
        }

        public static void AddRecordPlayerStats(ComponentPlayer componentPlayer, float time)
        {
            RecordPlayerStats record = new RecordPlayerStats();
            record.Time = time;
            record.Position = componentPlayer.ComponentBody.Position;
            record.Rotation = componentPlayer.ComponentBody.Rotation;
            record.LookAngles = componentPlayer.ComponentLocomotion.LookAngles;
            record.ActiveBlockValue = componentPlayer.ComponentMiner.ActiveBlockValue;
            record.Flying = componentPlayer.ComponentLocomotion.m_flying;
            record.Walking = componentPlayer.ComponentLocomotion.m_walking;
            record.Sneaking = componentPlayer.ComponentBody.IsSneaking;
            RecordPlayerStats.Add(record);
        }
    }
}